package cn.display.pixelworksx7.listener;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import cn.display.pixelworksx7.constant.GlobalData;
//import cn.display.pixelworksx7.service.HealthService;
import cn.display.pixelworksx7.threadpool.SystemThreadPool;
import cn.display.pixelworksx7.util.IrisConfigUtil;
import cn.display.pixelworksx7.util.ShellUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class TopAppListener extends AccessibilityService {
    private final String TAG = "AccessibilityService";

    public final List<String> ignoreList = Arrays.asList("com.oplus.games","com.android.systemui");
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        String topPackageName = ShellUtil.execCommand(
                "dumpsys activity activities|grep topResumedActivity=|tail -n 1|cut -d \"{\" -f2|cut -d \"/\" -f1|cut -d \" \" -f3",
                true
        ).successMsg;
        Log.d(TAG,"event:" + event.getPackageName());
        if (eventType==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            // 根据事件类型和包名进行相应的处理
            if (!topPackageName.equals(GlobalData.lastTopPackageName)) {
                GlobalData.lastTopPackageName = topPackageName;
                if (GlobalData.irisConfigMap.containsKey(topPackageName)) {
                    IrisConfigUtil.Companion.executeAppIrisConfig(topPackageName);
                } else {
                    IrisConfigUtil.Companion.disableIris();
                }
            }
        }
    }

    @Override
    public void onInterrupt() {
        Log.d("Accessibility", "Interrupted");
    }

    @Override
    protected void onServiceConnected() {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes =  AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
        ;
       // info.flags = AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        setServiceInfo(info);
        super.onServiceConnected();
    }
    
}
